/**
 * 
 */
package com.agilegroup.crm.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.widget.EditText;
import android.widget.RadioGroup;

/**
 * @author hisham
 *
 */
@SuppressLint("SimpleDateFormat")
public class Validation {

	private Activity activity;
	public Validation(Activity activity) {
		this.activity=activity;
	}
    public boolean isTimeValid(EditText txtStartTime,EditText txtEndTime,int messageId) {

    	String startTime = txtStartTime.getText().toString().trim();
    	String endTime = txtEndTime.getText().toString().trim();
        
        String[] startTimeSplit = startTime.split(":");
        Integer totalStartTime = (Integer.parseInt(startTimeSplit[0])*60)+
        							Integer.parseInt(startTimeSplit[1]);                
       
        String[] endTimeSplit = endTime.split(":");
        Integer totalEndTime = (Integer.parseInt(endTimeSplit[0])*60)+
        							Integer.parseInt(endTimeSplit[1]);
        if(totalStartTime<totalEndTime){
        	 return true;
        }
        else{
        	txtStartTime.setError(activity.getResources().getString(messageId));
        	 return false;
        }
    }
        
    public boolean isTextFieldNull(EditText textField,int messageId) {

    	String textBox = textField.getText().toString().trim();
    	if(textBox.length()==0){
    		textField.setError(activity.getResources().getString(messageId));
    		return false;
    	}
    	else{
    		return true;
    	}
    }
    
	public boolean isRadioButtonSelected(RadioGroup radioGroup){
    	int id = radioGroup.getCheckedRadioButtonId();
    	if(id==-1){
    		return false;
    	}
    	else{
    		return true;
    	}
    }
	public boolean isDateValid(EditText txtDate,EditText txtFollowUpDate,Integer messageId){
		DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		Date date;
		boolean sucess = false;
		Date followUpDate;
		try{
			String dateString=(txtDate.getText().toString());
			String followUpDateString=(txtFollowUpDate.getText().toString());
			date = formatter.parse(dateString);
			followUpDate = formatter.parse(followUpDateString);
			if(date.compareTo(followUpDate)>0){
				txtFollowUpDate.setError(activity.getResources().getString(messageId));
				sucess = false;
			}
			else if(date.compareTo(followUpDate)<0){
				sucess = true;
			}
			else{
				sucess = true;
			}
		}
		catch (java.text.ParseException e){e.printStackTrace();}
		return sucess;
    }
	public boolean isEmailValid(EditText email) {
	    if (email.getText()==null&&email.getText().equals("")) {
	        return false;
	    } else {
	        return android.util.Patterns.EMAIL_ADDRESS.matcher(email.getText()).matches();
	    }
	}
	
	public boolean isWebSiteValid(EditText website) {
	    if (website.getText()==null&&website.getText().equals("")) {
	        return false;
	    } else {
	        return android.util.Patterns.WEB_URL.matcher(website.getText()).matches();
	    }
	}
}
